package Engine;

import View.Load;
import View.Principal_;
import java.awt.Color;
import java.io.IOException;
import javax.swing.JOptionPane;

public class Engine {

    public void load() {
        new Thread() {
            public void run() {
                try {
                    sleep(50);
                    for (int i = 0; i <= 100; i++) {
                        sleep(5);
                        Load.jProgressBar1.setValue(i);
                        if (i == 100) {
                            Load.jLabel2.setText("Pronto");
                            Load.jProgressBar1.setVisible(false);
                            //instanciar principal tela
                            Principal_ principal = new Principal_();
                            principal.getContentPane().setBackground(new Color(255, 255, 255));
                            principal.setVisible(true);
                        }
                    }
                } catch (InterruptedException ex) {
                    JOptionPane.showMessageDialog(null, "Erro: " + ex);
                }
            }
        }.start();
    }

    public void command(String lineCommand_ref, String ip_ref) {
        String lineCommand = lineCommand_ref;
        String ip = ip_ref;
        try {
            Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL "
                    + "C:\\Windows\\System32\\cmd.exe /C "
                    + "ECHO " + lineCommand + ">\\\\" + ip + "\\LP1");
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao usar o Runtime. \n" + ex);
        }

    }

    public void commandShared(String lineCommand_ref) {
        String lineCommand = lineCommand_ref;
        String source = "C:\\Windows\\System32\\cmd.exe /C ECHO ";
        String prescribe = lineCommand;
        String exit = ">LPT1";
        String runtime = "rundll32 SHELL32.DLL,ShellExec_RunDLL ";
        String comando = source + prescribe + exit;
        try {
            Runtime.getRuntime().exec(runtime + comando);
            System.out.println(runtime + comando);
        } catch (IOException e) {
        }

    }

    public String testPattern() {
        return "!R!DELM CLR1;MCRO CLR1;SCG;SCF;SCCS;SCU;FTMD15;SIMG6;UNITD;"
                + "UOM600;SPO P;STR600;CID0,1;PAT1,600;ACLI1,236,0,140;SCOL1;"
                + "SROP168;PXPL1;NEWP;PMZP332,1430;PDRP1977,0;PDRP0,1977;"
                + "PDRP-1977,0;FILL1;PXPL0;ACLI1,0,0,0;SCOL1;PXPL1;NEWP;"
                + "PMZP2360,3444;PDRP1977,0;PDRP0,1977;PDRP-1977,0;FILL1;"
                + "PXPL0;ACLI1,255,242,0;SCOL1;PXPL1;NEWP;PMZP2360,1430;"
                + "PDRP1977,0;PDRP0,1977;PDRP-1977,0;FILL1;PXPL0;ACLI1,0,174,239;"
                + "SCOL1;PXPL1;NEWP;PMZP332,3444;PDRP1977,0;PDRP0,1977;"
                + "PDRP-1977,0;FILL1;RPU;RPCS;RPF;RPG;ENDM;RES;CALL CLR1;PAGE;EXIT;";
    }

    public String freeWrite(String free) {
        return free;
    }

    public String text(String text) {
        return "!R! TEXT '" + text + "'; EXIT;";
    }

    public String statusPage() {
        return "!R! STAT 0; EXIT;";
    }

    public String serviceStatusPage() {
        return "!R! STAT 1; EXIT;";
    }

    public String originalName() {
        return "!R! KCFG \"AREA\", 2; KCFG \"PRDT\", 2; EXIT;";
    }

    public String A4_LetterAnulate() {
        return "!R! FRPO S4, 1; EXIT;";
    }

    public String pressGOAnulate() {
        return "!R! FRPO Y0, 0; EXIT;";
    }

    public String resetAll() {
        return "!R! EGRE; EXIT;";
    }

    public String firmwareInt() {
        return "!R! FRPO INIT; EXIT;";
    }

    public String adjustFiscalNoteModule1() {
        return "!R! FRPO C1, 1; FRPO P1, 6; FRPO U0, 6; FRPO U1, 50; FRPO U8, 18; "
                + "FRPO D1, 1; FRPO D3, 1; FRPO N4, 0; EXIT;";
    }

    public String adjustFiscalNoteModule2() {
        return "!R! FRPO C1, 1; FRPO P1, 6; FRPO U0, 8; FRPO U1, 50; FRPO U8, 19; "
                + "FRPO D1, 1; FRPO D3, 1; FRPO N4, 2; EXIT;";
    }

    public String adjustCassetPaperSize(String casset_ref, String size_ref) {
        String varCasset = null;
        int varSize;
        if ("Bandeja 1".equals(casset_ref)) {
            varCasset = "T1";
        }

        if ("Bandeja 2".equals(casset_ref)) {
            varCasset = "T2";
        }

        if ("Bandeja 3".equals(casset_ref)) {
            varCasset = "T3";
        }

        if ("Bandeja 4".equals(casset_ref)) {
            varCasset = "T4";
        }

        if ("Bandeja MP".equals(casset_ref)) {
            varCasset = "R7";
        }

        if ("A4".equals(size_ref)) {
            varSize = 8;
            return "FRPO " + varCasset + ", " + varSize + "; EXIT;";
        }

        if ("Carta".equals(size_ref)) {
            varSize = 6;
            return "FRPO " + varCasset + ", " + varSize + "; EXIT;";
        }

        if ("Legal".equals(size_ref)) {
            varSize = 7;
            return "FRPO " + varCasset + ", " + varSize + "; EXIT;";
        }

        if ("B5".equals(size_ref)) {
            varSize = 9;
            return "FRPO " + varCasset + ", " + varSize + "; EXIT;";
        }

        if ("A5".equals(size_ref)) {
            varSize = 13;
            return "FRPO " + varCasset + ", " + varSize + "; EXIT;";
        }

        return null;

    }

    public void unMap() {
        String comando = "C:\\Windows\\System32\\cmd.exe /C "
                + "net use lpt1: /delete";
        try {
            Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + comando);
            Principal_.jTextField1.setText("");
            Principal_.jTextField1.setEditable(true);
            Principal_.jTextField1.setEnabled(true);
            Principal_.jLabel26.setVisible(false);
        } catch (IOException e) {
        }
    }

    public void map() {
        String ip = JOptionPane.showInputDialog("Digite o IP deste computador");
        String compartilhamento = JOptionPane.showInputDialog("Digite o nome do compartilhamento do sistema de impressão");
        String comando = "C:\\Windows\\System32\\cmd.exe /C "
                + "net use lpt1: \\\\" + ip + "\\" + compartilhamento;
        try {
            Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + comando);
        } catch (IOException e) {
        }        
        Principal_.jTextField1.setText(compartilhamento + " mapeado no IP: " + ip);
        Principal_.jTextField1.setEnabled(false);
        Principal_.jTextField1.setEditable(false);
        Principal_.jLabel26.setVisible(true);
    }
}
